package com.ec.vs.controller;

import com.ec.common.domain.Result;
import com.ec.vs.service.VsVisitService;
import com.ec.vs.vo.VsAppointmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理员-探访预约")
@RestController
@RequestMapping("/visit/admin/visit")
@RequiredArgsConstructor
@Slf4j
public class AdminVisitController {

    private final VsVisitService vsVisitService;   // 探访专用 service

    @ApiOperation("分页查询【探访】预约")
    @GetMapping
    public Result<Object> list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String mobile,
            @RequestParam(required = false) String elderName,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return vsVisitService.listVisitAppointments(name, mobile, elderName, status, page, size);
    }

    /* 1. 审核预约：待确定 -> 待完成 */
    @ApiOperation("审核通过【探访】预约")
    @PutMapping("/{id}/approve")
    public Result<String> approve(@PathVariable Long id) {
        int rows = vsVisitService.approveVisitAppointment(id);
        return rows > 0 ? Result.ok("审核成功") : Result.error("审核失败，请确认预约状态为待确定");
    }

    /* 2. 取消预约：状态置为 2 */
    @ApiOperation("取消【探访】预约")
    @PostMapping("/{id}/cancel")
    public Result<String> cancel(@PathVariable Long id) {
        int rows = vsVisitService.cancelVisitAppointment(id);
        return rows > 0 ? Result.ok("取消成功") : Result.error("取消失败");
    }

    @ApiOperation("删除【探访】预约")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        int rows = vsVisitService.deleteVisitAppointment(id);
        return rows > 0 ? Result.ok("删除成功") : Result.error("删除失败，预约不存在");
    }

    @ApiOperation("新增【探访】预约")
    @PostMapping
    public Result<String> create(@Valid @RequestBody VsAppointmentVo vo) {
        int rows = vsVisitService.createVisitAppointment(vo);
        return rows > 0 ? Result.ok("新增成功") : Result.error("新增失败");
    }
}